package ${packageName}.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*;
import ${packageName}.repository.${upperFirstLetterName}Repository;
import ${packageName}.model.${upperFirstLetterName};
import ${packageName}.service.${upperFirstLetterName}Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import ${packageName}.common.Page;
import javax.annotation.Generated;

@Generated(
	value = {
		"https://github.com/coolcooldee/sloth",
		"Sloth version:1.0"
	},
	comments = "This class is generated by Sloth"
)
@Service
public  class ${upperFirstLetterName}ServiceImpl implements ${upperFirstLetterName}Service{

	Logger logger = LoggerFactory.getLogger(${upperFirstLetterName}Service.class);

	@Autowired
	private ${upperFirstLetterName}Repository ${lowerFirstLetterName}Repository;

	public void save(${upperFirstLetterName} ${lowerFirstLetterName}){
		${lowerFirstLetterName}Repository.save(${lowerFirstLetterName});
	}

	@Override
	public ${upperFirstLetterName} getBy${upperFirstLetterPrimaryKey}(${primaryKeyType} ${primaryKey}){
		return ${lowerFirstLetterName}Repository.findOne(${primaryKey});
	}
	@Override
	public void deleteBy${upperFirstLetterPrimaryKey}(${primaryKeyType} ${primaryKey}){
		${lowerFirstLetterName}Repository.delete(${primaryKey});
	}
	@Override
	public void update(${upperFirstLetterName} ${lowerFirstLetterName}){
		${lowerFirstLetterName}Repository.save(${lowerFirstLetterName});
	}

	@Override
	public Long count(){
	    return ${lowerFirstLetterName}Repository.count();
	}

	@Override
	public List<${upperFirstLetterName}> list(){
    List<${upperFirstLetterName}> ${lowerFirstLetterName}s = new ArrayList<${upperFirstLetterName}>();
        Iterable<${upperFirstLetterName}> ${lowerFirstLetterName}Iterator = ${lowerFirstLetterName}Repository.findAll();
		Iterator<${upperFirstLetterName}> iterator =  ${lowerFirstLetterName}Iterator.iterator();
		while(iterator.hasNext()){
			${upperFirstLetterName} ${lowerFirstLetterName} = iterator.next();
			${lowerFirstLetterName}s.add(${lowerFirstLetterName});
        }
        return ${lowerFirstLetterName}s;
	}

	public Page<${upperFirstLetterName}> page(int pageNo, int pageSize) {
    	Pageable pageable = new PageRequest(pageNo, pageSize);
    	org.springframework.data.domain.Page<${upperFirstLetterName}> sPages = ${lowerFirstLetterName}Repository.findAll(pageable);
        List<${upperFirstLetterName}> rList = new ArrayList<${upperFirstLetterName}>();
		Iterator<${upperFirstLetterName}> iterator = sPages.iterator();
        while (iterator.hasNext()){
			${upperFirstLetterName} temp = iterator.next();
           rList.add(temp);
        }
        Page<${upperFirstLetterName}> mPage = new Page<${upperFirstLetterName}>();
        mPage.setTotalCount(sPages.getTotalElements());
		mPage.setStart(pageNo*pageSize+1);
        mPage.setPageSize(pageSize);
        mPage.setResult(rList);
        return mPage;
    }

}